//
//  ProductViewController.m
//  BosidengIphone
//
//  Created by  on 11-12-29.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "ProductViewController.h"
#import "ProductData.h"
#import "ProductDetailViewController.h"
#import "MainViewController.h"
#import "AppDelegate.h"
#import "MainViewController.h"
#import "SyncImageView.h"

@implementation ProductViewController
@synthesize products;
@synthesize productName;
@synthesize brandCategoryLabel;
@synthesize selectBrandData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //self.products = [ProductData getAllItems];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    [selectBrandData release];
    [brandCategoryLabel release];
    [categoryView release];
    [productName release];
    [products release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    BrandData *defaultCategoryData = nil;
    NSArray *brandCategorys = [BrandData getAllItems];
    for (BrandData *categoryData in brandCategorys) {
        if ([@"1" isEqualToString:categoryData.brandID]) {
            categoryData.name = [AppDelegate getLanguageString:@"brand_category_all"];
            defaultCategoryData = categoryData;
        }
    }
    categoryView = [[AboutCategoryViewController alloc] initWithNibName:@"AboutCategoryViewController" bundle:nil];
    categoryView.brands = brandCategorys;
    categoryView.delegate = self;
    categoryView.view.frame = CGRectMake(0, -392, 320, 392);
    [self.view addSubview:categoryView.view];
    
    if (selectBrandData) {
        brandCategoryLabel.text = [NSString stringWithFormat:@"%@ (%d)", selectBrandData.name, [products count]];
    } else {
        if (defaultCategoryData) {
            brandCategoryLabel.text = [NSString stringWithFormat:@"%@ (%d)", defaultCategoryData.name, [products count]];
        }
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)showMenuAnimation:(UIView*)view {
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:1];
	[UIView setAnimationDelay:0.0];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 416);
    view.transform = transform;
	[UIView commitAnimations];
}

- (IBAction)showBrandSelect:(id)sender {
    [self showMenuAnimation:categoryView.view];
}

#pragma mark - PhotoScrollViewDelegate

- (NSInteger)imageCount {
    return [products count];
}

- (UIView *)configureView:(UIView *)page atIndex:(NSInteger)index {
    ProductData *data = [products objectAtIndex:index];
    NSString *url = [data getHeadImageUrl];
    if (page == nil) {
        page = [[[SyncImageView alloc] init] autorelease];
    }
    [(SyncImageView *)page loadImage:url andRequestQueue:requestQueue];
    return page;
}

- (void)changePageIndex:(NSInteger)index {
    ProductData *data = [products objectAtIndex:index];
    productName.text = [NSString stringWithFormat:@"%@ %@", data.name, data.item];
}

- (void)selectPageIndex:(NSInteger)index {
    ProductDetailViewController *viewController = [[ProductDetailViewController alloc] initWithNibName:@"ProductDetailViewController" bundle:nil];
    viewController.product = [products objectAtIndex:index];
    viewController.brand = [BrandData getItemByID:viewController.product.brandID];
    [(MainViewController *)mainViewController pushViewController:viewController];
    [viewController release];
}

#pragma mark - BrandCategoryViewDelegate

- (void)changeViewController:(BrandData *)data {
    if (selectBrandData) {
        if ([selectBrandData.brandID isEqualToString:data.brandID]) {
            return;
        }
    }
    ProductViewController *viewController = [[ProductViewController alloc] initWithNibName:@"ProductViewController" bundle:nil];
    if ([@"1" isEqualToString:data.brandID]) {
        viewController.products = [ProductData getAllItems];
    } else {
        viewController.products = [ProductData getItemsByBrandId:data.brandID];
    }
    viewController.selectBrandData = data;
    [(MainViewController *)mainViewController changeTopViewController:viewController];
    [viewController release];
}

@end
